package cn.tq365.util.baidu;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONArray;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.struts2.ServletActionContext;

/**
 * 百度常量工具类
 * 
 * @author lili
 * 
 */
public class AsBaiduConstUtil {
	/*
	 * 搜索网络地域常量列表
	 */
	public static final Map<String, String> searchRegions = new HashMap<String, String>();
	/*
	 * 网盟地域常量列表
	 */
	public static final Map<String, String> contentRegions = new HashMap<String, String>();

	/*
	 * 默认地域常量列表文件路径
	 */
	// public static final String defaultPath = Thread.currentThread()
	// .getContextClassLoader().getResource("").toString().toString()
	// .replace("file:/", "")
	// + "baiduRegions.xlsx";
	/*
	 * 地域常量列表文件路径
	 */
	public static final String path = ServletActionContext.getRequest()
			.getRealPath("/upload/const/baiduRegions.xlsx");

	static {
		update(null);
	}

	public static boolean update(File file) {
		try {
			FileInputStream in = new FileInputStream(path);
			if (file != null) {
				in = new FileInputStream(file);
			}
			XSSFWorkbook wb = new XSSFWorkbook(in);
			// 获取搜索地域列表
			XSSFSheet sSheet = wb.getSheetAt(0);
			// 获取网盟地域列表
			XSSFSheet cSheet = wb.getSheetAt(1);
			int sRows = sSheet.getLastRowNum() + 1;
			int cRows = cSheet.getLastRowNum() + 1;
			XSSFRow row = null;
			XSSFCell nameCell = null;
			XSSFCell codeCell = null;
			for (int i = 1; i <= sRows; i++) {
				row = sSheet.getRow(i);
				if (row != null) {
					nameCell = row.getCell(0);
					codeCell = row.getCell(1);
					searchRegions.put(String.valueOf(((Double) codeCell
							.getNumericCellValue()).longValue()), nameCell
							.getStringCellValue());
				} else {
					continue;
				}
			}
			for (int i = 1; i <= cRows; i++) {
				row = cSheet.getRow(i);
				if (row != null) {
					nameCell = row.getCell(2);
					codeCell = row.getCell(1);
					contentRegions.put(String.valueOf(((Double) codeCell
							.getNumericCellValue()).longValue()), nameCell
							.getStringCellValue());
				} else {
					continue;
				}
			}
			System.out.println(JSONArray.fromObject(searchRegions));
			System.out.println(JSONArray.fromObject(contentRegions));
			if (file != null) {
				in = new FileInputStream(file);
				FileOutputStream out = null;
				try {
					out = new FileOutputStream(path);
					byte[] buff = new byte[1024];
					int len = in.read(buff);
					while (len != -1) {
						out.write(buff, 0, len);
						len = in.read(buff);
					}
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					try {
						in.close();
						out.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

}
